'use strict';
module.exports = {
    parseInt(string) {
        if (typeof string === 'number') return string;
        if (!string) return string;
        return parseInt(string) || 0;
    },
    result(data, code, msg) {
        return Object.assign({}, {
            code: code || 0,
            message: msg ? msg : "",
            data,
        });
    },
    success(data,msg) {
        return this.result(data, 0, msg);
    },
    fail(msg, data) {
        data = data || null;
        return this.result(data, -1, msg);
    },
    sleep(delay){  //延时函数
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                try {
                    resolve(1)
                } catch (e) {
                    reject(0)
                }
            }, delay)
        })
    },
    generateSecret(len) {
        // 生成密钥
        let result = '';
        const chars = '0123456789abcdefghigklmnopqrstuvwxyz';
        for (let i = len; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
        return result;
    },
};